package com.lute.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.security.oauth2.common.json.JSONException;
import org.springframework.security.oauth2.common.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lute.model.Employee;
import com.lute.model.MailTemplate;
import com.lute.utils.SendMail;
import com.lute.utils.ServerErrorResponse;

@Controller
public class EmployeeProfileUpdateEmployeeServlet {

	@RequestMapping(value="/employeeProfileUpdateEmployee", method= RequestMethod.POST, headers="Content-type=application/json") 
	public @ResponseBody String employeeProfileUpdateEmployee(@RequestBody String jsonFromUI, HttpServletRequest request) throws JSONException {
		String result = "";
		JSONObject jsonReq;
		JSONObject jsonRes = new JSONObject();
		
		HttpSession session = request.getSession(false);
		if(session == null) {
			jsonRes.put("result", ServerErrorResponse.USER_NOT_LOGGED_IN.toString());
			result = jsonRes.toString();
			return result;
		}

		try {
			jsonReq = new JSONObject(jsonFromUI);
			jsonRes = new JSONObject();
		} catch (JSONException e){
			jsonRes = new JSONObject();
			jsonRes.put("result", ServerErrorResponse.MALFORMED_REQUEST.toString());
			result = jsonRes.toString();
			return result;
		}
		
		String email = jsonReq.getString("email");
		
		// withdraw from session: EmployeeID, role
		int sessionUserId = (Integer)session.getAttribute("sessionUserId");
		
		// value for GETPERSON.JSP testing
		// int sessionUserId = 4;
		String sessionUserRole = (String)session.getAttribute("role");
		
		// value for GETPERSON.JSP testing
		// String sessionUserRole = "employee";
		try {
		
			if (sessionUserRole.equals("employee")) {
				// email validation, email change, sending test mail
				if (SendMail.getInstance().isValidEmailAddress(email)) {
					if (Employee.updateEmail("id_user", sessionUserId, sessionUserRole, email)) {
						Employee employee = Employee.getEmployeeFromDB("id_user", sessionUserId);
						
						String toSubject = MailTemplate.NEW_EMPLOYEE_TO_EMPLOYEE_SUBJECT.toString();
						toSubject = toSubject + " " + employee.getFirst_name() + " " + employee.getLast_name();
						String toBody = MailTemplate.EMPLOYEE_CHANGED_EMAIL_ADDRESS_BODY.toString();
						
						SendMail.getInstance().sendMailTo(email,toSubject,toBody);
						
						jsonRes.put("result", "OK");
						result = jsonRes.toString();
					} else {
						jsonRes.put("result", ServerErrorResponse.MALFORMED_REQUEST.toString());
						result = jsonRes.toString();
						return result;
					}
				} else {
					jsonRes.put("result", ServerErrorResponse.INVALID_EMAIL.toString());
					result = jsonRes.toString();
					return result;
				}
			} else {
				jsonRes.put("result", ServerErrorResponse.USER_NOT_ENTITLED.toString());
				result = jsonRes.toString();
				return result;
			}
		}catch(NullPointerException npe) {
			jsonRes.put("result", ServerErrorResponse.INTERNAL_ERROR.toString());
			result = jsonRes.toString();
			return result;
		}
		return result;
	}
}
